﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.Exchange
{
    public partial class saleDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

            if (!Page.IsPostBack)
            {
                BindData(Convert.ToInt32(Request.QueryString["sid"]));

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if(!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the data of a sale based on the input parameter.
        /// </summary>
        /// <param name="saleId">Specifies the ID of the sale to be bound.</param>
        private void BindData(int saleId)
        {
            Sale sal = dbExchange.GetSaleDetails(saleId);

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            lblSaleDetailsTitle.Text = sal.Title;
            lblSaleDetailsDate.Text = sal.CreatedDate.ToString("dd-MM-yyyy") + ",";
            hyLinkSaleDetailsUser.Text = sal.User.UserName;
            hyLinkSaleDetailsUser.NavigateUrl = "~/User/profileDetails.aspx?uid=" + sal.User.UserID;
            
            if(!culture.Equals("en-GB"))
                lblSaleDetailsCategory.Text = sal.Category.CategoryName;
            else
                lblSaleDetailsCategory.Text = sal.Category.LocalizedCategoryName;

            lblSaleDetailsDescription.Text = sal.Description;
            lblSaleDetailsName.Text = sal.User.FirstName + " " + sal.User.LastName;
            lblSaleDetailsPhone.Text = sal.User.Phone;
            lblSaleDetailsEmail.Text = sal.User.Email;
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            lblSaleDetailsContactPerson.Text = GetLocalResourceObject("lblSaleDetailsContactPersonText").ToString();
            lblSaleDetailsEmailHeader.Text = GetLocalResourceObject("lblSaleDetailsEmailHeaderText").ToString();
            lblSaleDetailsNameHeader.Text = GetLocalResourceObject("lblSaleDetailsNameHeaderText").ToString();
            lblSaleDetailsPhoneHeader.Text = GetLocalResourceObject("lblSaleDetailsPhoneHeaderText").ToString();
        }
    }
}